/*
 * AnswersServicePanel.java
 *
 * Created on September 22, 2008, 1:33 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.ido.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Example class using the AnswersService service. 
 *
 * @author greenido
 */
public class AnswersServicePanel extends HorizontalPanel {

    private Button rightButton = new Button("Correct");
    private Button wrongButton = new Button("Wrong");
    
    public AnswersServicePanel() {

        add(rightButton);
        add(wrongButton);
        
        // Create an asynchronous callback to handle the result.
        final AsyncCallback callback = new AsyncCallback() {
            public void onSuccess(Object result) {
                // bla=bla
            }
            
            public void onFailure(Throwable caught) {
                //bla-bla
            }
        };
        
        // Listen for the button clicks
        rightButton.addClickListener(new ClickListener(){
            public void onClick(Widget w) {
                // Make remote call. Control flow will continue immediately and later
                // 'callback' will be invoked when the RPC completes.
                getService().myMethod("todo", callback);
            }
        });
    }
    
    public static AnswersServiceAsync getService(){
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of
        // the interface. The cast is always safe because the generated proxy
        // implements the asynchronous interface automatically.
        AnswersServiceAsync service = (AnswersServiceAsync) GWT.create(AnswersService.class);
        // Specify the URL at which our service implementation is running.
        // Note that the target URL must reside on the same domain and port from
        // which the host page was served.
        //
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "answersservice";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }
}
